const express = require("express")
const router = express.Router()
let db = require("../db.js")




// 新增
router.post("/add", function (req, res) {
    console.log(1);
    db("insert into todo set ?", req.body, function (err, obj) {
        console.log(err);
        if (err != null) {
            res.send({
                status: 1,
                message: "新增失败",
            });
        } else {
            res.send({
                status: 0,
                message: "新增成功",
            })
        }
    })
})


// 列表
router.get("/list", function (req, res) {
    db("select * from todo", null, function (err, arr) {
        if (err != null) {
            res.send({
                status: 1,
                message: "获取列表失败"
            });
        } else {
            res.send({
                status: 0,
                message: "获取列表成功",
                arr: arr
            })
        }
    })
})




// 删除
router.get("/del/:id", function (req, res) {
    let Id = req.params.id;

    db("delete from todo where id=?", Id, function (err, obj) {
        if (err != null) {
            res.send({
                status: 1,
                message: "删除失败",
            });
        } else {
            res.send({
                status: 0,
                message: "删除成功",
            })
        }
    })
})



// 更新
router.post("/edit", function (req, res) {

    db("update todo set ? where id=?", [req.body, req.body.id], function (err, obj) {
        if (err != null) {
            res.send({
                status: 1,
                message: "更新失败",
            });
        } else {
            res.send({
                status: 0,
                message: "更新成功",
            })
        }
    })
})
module.exports = router;